/*
 * @Descripttion: 
 * @version: 
 * @Author: jwZhang
 * @Date: 2020-04-30 20:44:32
 * @LastEditors: jwZhang
 * @LastEditTime: 2020-05-10 16:45:57
 */
import 'package:cloud_music/application.dart';
import 'package:cloud_music/core/datamodels/user.dart';
import 'package:cloud_music/route/navigate_service.dart';
import 'package:cloud_music/route/routes.dart';
import 'package:cloud_music/utils/net_utils.dart';
import 'package:cloud_music/utils/utils.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

import '../service_url.dart';

class UserRequest {
  // 登录
  static Future<User> login(BuildContext context, String account,
      String password, int accountType) async {
    var response = await NetUtils.get(context, ServiceUrl.login,
        params: {'phone': account, 'password': password});
    return User.fromJson(response.data);
  }

  // 刷新登录信息
  static Future<Response> refreshLogin(BuildContext context) async {
    return await NetUtils.get(context, '/login/refresh', isShowLoading: false)
        .catchError((e) {
      Utils.showToast('网络错误！');
    });
  }

  static void reLogin() {
    Future.delayed(Duration(milliseconds: 200), () {
      Application.locator<NavigateService>().popAndPushNamed(Routes.login);
      Utils.showToast('登录失效，请重新登录');
    });
  }
}
